//1.引入/导入/加载 http模块
const http = require('http');
//2.创建服务器对象
const server = http.createServer();
//3.监听服务器对象，并启动服务器
server.listen(3000,()=>{
  console.log('server is runing...');
});
//4.1 加载 fs 模块
const fs = require('fs');
//4.给服务器对象注册request事件
//127.0.0.1:3000/detail ,显示 detail.html 页面
server.on('request',(req,res)=>{
  const url = req.url;
  console.log(url)
  if(url ==='/detail'){
    fs.readFile('./view/detail.html',(err,data)=>{
      if(err){
        console.log(err);
        return res.end('404 not found');
      }
      res.end(data);
    })
  }else if(url === '/public/css/a.css'){
    fs.readFile('./public/css/a.css',(err,data)=>{
      if(err){
        console.log(err);
        return res.end('404 not found');
      }
      res.end(data);
    })
  }else if(url === '/public/js/b.js'){
    fs.readFile('./public/js/b.js',(err,data)=>{
      if(err){
        console.log(err);
        return res.end('404 not found');
      }
      res.end(data);
    })
  }else if(url === '/public/img/1.jpg'){
    fs.readFile('./public/img/1.jpg',(err,data)=>{
      if(err){
        console.log(err);
        return res.end('404 not found');
      }
      res.end(data);
    })
  }
});